#include <my_header.h>
#include <string.h>
#define BUF_SIZE 4096

/* Usage: mkfifo 创建管道文件 */
int main(int argc, char *argv[]){    
    int fd_read = open("1.pipe", O_RDONLY);
    ERROR_CHECK(fd_read, -1, "fd2_read");
    int fd_write = open("2.pipe", O_WRONLY);
    ERROR_CHECK(fd_write, -1, "fd2_write");
    char sendbuf[BUF_SIZE] = {0};
    char recvbuf[BUF_SIZE] = {0};

    while(1){
        // B read pipe and print
        bzero(recvbuf, sizeof(recvbuf));
        ssize_t read_count1 = read(fd_read, recvbuf, sizeof(recvbuf));
        if(read_count1 <= 0) break;
        recvbuf[read_count1] = '\0';
        printf("B recv > %s ", recvbuf);
        fflush(stdout);

        // B input and send pipe
        bzero(sendbuf, sizeof(sendbuf));
        printf("B input > ");
        fflush(stdout);
        
        ssize_t read_count2 = read(STDIN_FILENO, sendbuf, sizeof(sendbuf));
        if(read_count2 <= 0) break;     
        write(fd_write, sendbuf, sizeof(sendbuf));
        sendbuf[read_count2] = '\0';
    }

    close(fd_write);
    close(fd_read);
    return 0;
}

